package ProvaView;

import Media.MediaController;
import Prova.Prova;
import Prova.ProvaException;
import Prova.QuestaoProva;
import java.util.Arrays;

public class TelaQuestaoProva extends javax.swing.JFrame
{
    private QuestaoProva questaoProva;
    private int numeroQuestoes;
    public static int[] posicoes = null;

    public QuestaoProva getQuestaoProva() {
        return questaoProva;
    }

    public void setQuestaoProva(QuestaoProva questaoProva) {
        this.questaoProva = questaoProva;
    }

    public int getNumeroQuestoes() {
        return numeroQuestoes;
    }

    public void setNumeroQuestoes(int numeroQuestoes) {
        this.numeroQuestoes = numeroQuestoes;
    }

    /** Creates new form TelaQuestaoProva */
    public TelaQuestaoProva(QuestaoProva questaoProva, int numeroQuestoes) {
        initComponents();
        this.questaoProva = questaoProva;
        this.numeroQuestoes = numeroQuestoes;
        
        if(posicoes == null)
        {
            posicoes = new int[numeroQuestoes];
            for (int posicao= 0; posicao < posicoes.length; posicao++)
                posicoes[posicao] = posicao+1;
        }
        
        if (questaoProva.getQuestao() != null)
        {
            int posicaoAtual = questaoProva.getPosicao();
            if(posicaoAtual != 0)
            {
                posicoes = Arrays.copyOf(posicoes, posicoes.length+1);
                posicoes[posicoes.length-1] = posicaoAtual;
                Arrays.sort(posicoes);
            }

            tbxQuestao.setText(questaoProva.getQuestao().getPergunta());
            tbxPontuacao.setText(String.valueOf(questaoProva.getPontuacao()));

            for(int posicao = 0; posicao < posicoes.length; posicao++)
                cbxPosicao.addItem(posicoes[posicao]);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbProva = new javax.swing.JLabel();
        lbPontuacao = new javax.swing.JLabel();
        lbQuestao = new javax.swing.JLabel();
        tbxPontuacao = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbxQuestao = new javax.swing.JTextArea();
        btnConfirmar = new javax.swing.JButton();
        cbxPosicao = new javax.swing.JComboBox();
        Posição = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lbProva.setText("Prova");

        lbPontuacao.setText("Pontuação");

        lbQuestao.setText("Questao");

        tbxPontuacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbxPontuacaoActionPerformed(evt);
            }
        });
        tbxPontuacao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbxPontuacaoKeyTyped(evt);
            }
        });

        tbxQuestao.setColumns(20);
        tbxQuestao.setEditable(false);
        tbxQuestao.setRows(5);
        tbxQuestao.setEnabled(false);
        jScrollPane1.setViewportView(tbxQuestao);

        btnConfirmar.setText("Confirmar");
        btnConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmarActionPerformed(evt);
            }
        });

        cbxPosicao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxPosicaoActionPerformed(evt);
            }
        });

        Posição.setText("Posição");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(164, 164, 164)
                        .addComponent(lbProva))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(158, 158, 158)
                        .addComponent(btnConfirmar))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lbQuestao))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lbPontuacao)
                                .addGap(18, 18, 18)
                                .addComponent(tbxPontuacao, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
                                .addGap(47, 47, 47)
                                .addComponent(Posição)
                                .addGap(18, 18, 18)
                                .addComponent(cbxPosicao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(87, 87, 87)))
                        .addGap(23, 23, 23)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lbProva)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbPontuacao)
                    .addComponent(Posição)
                    .addComponent(cbxPosicao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tbxPontuacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(lbQuestao)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addComponent(btnConfirmar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmarActionPerformed
        Prova prova = new Prova();

        try
        {
            int posicao = (Integer)cbxPosicao.getSelectedItem();
            questaoProva.setPosicao(posicao);
            repopulaArrayPosicoesQuestoes(posicao);

            if (!(tbxPontuacao.getText().isEmpty()))
            {
                double pontuacao = Double.parseDouble(tbxPontuacao.getText());
                if(prova.verificaPontuacaoQuestao(pontuacao))
                    questaoProva.setPontuacao(pontuacao);
            }

            dispose();
        }
        catch(ProvaException ep)
        {
            MediaController.showErrorMessage(ep.getMessage(), "Erro!");
        }
        catch(Exception e)
        {
            MediaController.showErrorMessage(e.getMessage(), "Erro!");
        }
    }//GEN-LAST:event_btnConfirmarActionPerformed

    private void tbxPontuacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbxPontuacaoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tbxPontuacaoActionPerformed

    private void tbxPontuacaoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbxPontuacaoKeyTyped
        char caracterDigitado = evt.getKeyChar();
        if (!Character.isDigit(caracterDigitado) && caracterDigitado != '.')
               evt.setKeyChar('\u0000');
    }//GEN-LAST:event_tbxPontuacaoKeyTyped

    private void cbxPosicaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxPosicaoActionPerformed
        // TODO add your handling code here:

    }//GEN-LAST:event_cbxPosicaoActionPerformed


    public void repopulaArrayPosicoesQuestoes(int posicaoUtilizada)
    {
        int[] newArrayPosicoes = new int[posicoes.length -1];
        int i = 0;

        for(int posicao : posicoes)
        {
            if(posicao == posicaoUtilizada)
                continue;
            else
            {
                newArrayPosicoes[i] = posicao;
                i++;
            }
        }

        posicoes = newArrayPosicoes;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Posição;
    private javax.swing.JButton btnConfirmar;
    private javax.swing.JComboBox cbxPosicao;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbPontuacao;
    private javax.swing.JLabel lbProva;
    private javax.swing.JLabel lbQuestao;
    private javax.swing.JTextField tbxPontuacao;
    private javax.swing.JTextArea tbxQuestao;
    // End of variables declaration//GEN-END:variables
}
